package jst.householdmanager.activities;

import jst.householdmanager.R;
import jst.householdmanager.database.ItemCategoriesDataSource;
import jst.householdmanager.database.Items_in_ItemCategoriesDataSource;
import jst.householdmanager.database.table.ItemCategories;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditItemCategoryAct extends Activity {

	private int selectedItemCategoryId = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_edit_itemcategory);

		EditText edt_name = ((EditText) findViewById(R.id.edit_shop_edt_name));
		edt_name.requestFocus();

		Bundle givenData = getIntent().getExtras();

		if (givenData != null) {
			this.selectedItemCategoryId = givenData.getInt("ItemCategoryId");

			ItemCategories selectedItemCategory = ItemCategoriesDataSource.read(this.selectedItemCategoryId);
			edt_name.setText(selectedItemCategory.getName());
		}

		getActionBar().setDisplayHomeAsUpEnabled(true);

		edt_name.setOnEditorActionListener(new EditText.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					EditItemCategoryAct.this.saveChanges();
				}
				return true;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.edit_action, menu);
		if (this.selectedItemCategoryId == -1) {
			MenuItem item = menu.findItem(R.id.mnu_delete);
			item.setEnabled(false);
			item.setVisible(false);
		}
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			break;
		case R.id.mnu_save:
			this.saveChanges();
			break;
		case R.id.mnu_delete:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(getText(R.string.are_you_sure));
			builder.setMessage(getText(R.string.delete_itemcategory));
			// yes
			builder.setPositiveButton(getText(R.string.yes), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// delete Items in ItemCategories
					Items_in_ItemCategoriesDataSource.delete(Items_in_ItemCategoriesDataSource.COLUMN_ITMCAT_ID + " = ?",
							EditItemCategoryAct.this.selectedItemCategoryId);
					// delete ItemCategory
					ItemCategoriesDataSource.delete(new ItemCategories(EditItemCategoryAct.this.selectedItemCategoryId));
					dialog.dismiss();
					Intent intent = new Intent();
					intent.putExtra("dataChanged", true);
					setResult(RESULT_OK, intent);
					finish();
				}
			});
			// no
			builder.setNegativeButton(getText(R.string.no), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	private void saveChanges() {
		EditText edt_name = (EditText) findViewById(R.id.edit_shop_edt_name);
		if (edt_name.getText().toString().matches("")) {
			Toast.makeText(this, R.string.required_insert_name, Toast.LENGTH_LONG).show();
		} else {
			ItemCategories foundItemCategory = ItemCategoriesDataSource.read(ItemCategoriesDataSource.COLUMN_NAME + " = ?", new String[] { edt_name
					.getText().toString() });
			if (foundItemCategory == null) {
				ItemCategoriesDataSource.save(new ItemCategories(this.selectedItemCategoryId, edt_name.getText().toString()));
				// return data changed flag
				Intent intent = new Intent();
				intent.putExtra("dataChanged", true);
				setResult(RESULT_OK, intent);
				finish();
				// Same entry without change
			} else if (this.selectedItemCategoryId == foundItemCategory.getId()) {
				// return data changed flag
				Intent intent = new Intent();
				intent.putExtra("dataChanged", false);
				setResult(RESULT_OK, intent);
				finish();
			} else {
				Toast.makeText(this, R.string.duplicate_entry, Toast.LENGTH_LONG).show();
			}
		}
	}

}
